/*     */ package com.cr.generator.util;
/*     */ 
/*     */ import com.alibaba.fastjson.JSONObject;
/*     */ import com.cr.common.utils.MyDateUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.generator.config.GenConfig;
/*     */ import com.cr.generator.domain.GenTable;
/*     */ import com.cr.generator.domain.GenTableColumn;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.velocity.VelocityContext;
/*     */ 
/*     */ public class VelocityUtils {
/*     */   private static final String PROJECT_PATH = "main/java";
/*     */   
/*     */   private static final String MYBATIS_PATH = "main/resources/mapper";
/*     */   
/*     */   private static final String TEMPLATES_PATH = "main/resources/templates";
/*     */   
/*     */   private static final String DEFAULT_PARENT_MENU_ID = "3";
/*     */   
/*     */   public static VelocityContext prepareContext(GenTable genTable) {
/*  36 */     String moduleName = genTable.getModuleName();
/*  37 */     String businessName = genTable.getBusinessName();
/*  38 */     String packageName = genTable.getPackageName();
/*  39 */     String tplCategory = genTable.getTplCategory();
/*  40 */     String functionName = genTable.getFunctionName();
/*  42 */     VelocityContext velocityContext = new VelocityContext();
/*  43 */     velocityContext.put("tplCategory", genTable.getTplCategory());
/*  44 */     velocityContext.put("tableName", genTable.getTableName());
/*  45 */     velocityContext.put("functionName", MyStringUtils.isNotEmpty(functionName) ? functionName : "【请填写功能名称】");
/*  46 */     velocityContext.put("ClassName", genTable.getClassName());
/*  47 */     velocityContext.put("className", MyStringUtils.uncapitalize(genTable.getClassName()));
/*  48 */     velocityContext.put("moduleName", genTable.getModuleName());
/*  49 */     velocityContext.put("businessName", genTable.getBusinessName());
/*  50 */     velocityContext.put("basePackage", getPackagePrefix(packageName));
/*  51 */     velocityContext.put("packageName", packageName);
/*  52 */     velocityContext.put("author", genTable.getFunctionAuthor());
/*  53 */     velocityContext.put("datetime", MyDateUtils.getDate());
/*  54 */     velocityContext.put("pkColumn", genTable.getPkColumn());
/*  55 */     velocityContext.put("importList", getImportList(genTable));
/*  56 */     velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
/*  57 */     velocityContext.put("columns", genTable.getColumns());
/*  58 */     velocityContext.put("table", genTable);
/*  59 */     setMenuVelocityContext(velocityContext, genTable);
/*  60 */     if ("tree".equals(tplCategory))
/*  62 */       setTreeVelocityContext(velocityContext, genTable); 
/*  64 */     if ("sub".equals(tplCategory))
/*  66 */       setSubVelocityContext(velocityContext, genTable); 
/*  68 */     return velocityContext;
/*     */   }
/*     */   
/*     */   public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) {
/*  73 */     String options = genTable.getOptions();
/*  74 */     JSONObject paramsObj = JSONObject.parseObject(options);
/*  75 */     String parentMenuId = getParentMenuId(paramsObj);
/*  76 */     context.put("parentMenuId", parentMenuId);
/*     */   }
/*     */   
/*     */   public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) {
/*  81 */     String options = genTable.getOptions();
/*  82 */     JSONObject paramsObj = JSONObject.parseObject(options);
/*  83 */     String treeCode = getTreecode(paramsObj);
/*  84 */     String treeParentCode = getTreeParentCode(paramsObj);
/*  85 */     String treeName = getTreeName(paramsObj);
/*  87 */     context.put("treeCode", treeCode);
/*  88 */     context.put("treeParentCode", treeParentCode);
/*  89 */     context.put("treeName", treeName);
/*  90 */     context.put("expandColumn", Integer.valueOf(getExpandColumn(genTable)));
/*  91 */     if (paramsObj.containsKey("treeParentCode"))
/*  93 */       context.put("tree_parent_code", paramsObj.getString("treeParentCode")); 
/*  95 */     if (paramsObj.containsKey("treeName"))
/*  97 */       context.put("tree_name", paramsObj.getString("treeName")); 
/*     */   }
/*     */   
/*     */   public static void setSubVelocityContext(VelocityContext context, GenTable genTable) {
/* 103 */     GenTable subTable = genTable.getSubTable();
/* 104 */     String subTableName = genTable.getSubTableName();
/* 105 */     String subTableFkName = genTable.getSubTableFkName();
/* 106 */     String subClassName = genTable.getSubTable().getClassName();
/* 107 */     String subTableFkClassName = MyStringUtils.convertToCamelCase(subTableFkName);
/* 109 */     context.put("subTable", subTable);
/* 110 */     context.put("subTableName", subTableName);
/* 111 */     context.put("subTableFkName", subTableFkName);
/* 112 */     context.put("subTableFkClassName", subTableFkClassName);
/* 113 */     context.put("subTableFkclassName", MyStringUtils.uncapitalize(subTableFkClassName));
/* 114 */     context.put("subClassName", subClassName);
/* 115 */     context.put("subclassName", MyStringUtils.uncapitalize(subClassName));
/* 116 */     context.put("subImportList", getImportList(genTable.getSubTable()));
/*     */   }
/*     */   
/*     */   public static List<String> getTemplateList(String tplCategory) {
/* 126 */     List<String> templates = new ArrayList<>();
/* 127 */     templates.add("vm/java/domain.java.vm");
/* 128 */     templates.add("vm/java/mapper.java.vm");
/* 129 */     templates.add("vm/java/service.java.vm");
/* 130 */     templates.add("vm/java/serviceImpl.java.vm");
/* 131 */     templates.add("vm/java/controller.java.vm");
/* 132 */     templates.add("vm/xml/mapper.xml.vm");
/* 133 */     if ("crud".equals(tplCategory)) {
/* 135 */       templates.add("vm/html/list.html.vm");
/* 137 */     } else if ("tree".equals(tplCategory)) {
/* 139 */       templates.add("vm/html/tree.html.vm");
/* 140 */       templates.add("vm/html/list-tree.html.vm");
/* 142 */     } else if ("sub".equals(tplCategory)) {
/* 144 */       templates.add("vm/html/list.html.vm");
/* 145 */       templates.add("vm/java/sub-domain.java.vm");
/*     */     } 
/* 147 */     templates.add("vm/html/add.html.vm");
/* 148 */     templates.add("vm/html/edit.html.vm");
/* 149 */     templates.add("vm/sql/sql.vm");
/* 150 */     return templates;
/*     */   }
/*     */   
/*     */   public static String getFileName(String template, GenTable genTable) {
/* 159 */     String fileName = "";
/* 161 */     String packageName = genTable.getPackageName();
/* 163 */     String moduleName = genTable.getModuleName();
/* 165 */     String className = genTable.getClassName();
/* 167 */     String businessName = genTable.getBusinessName();
/* 169 */     String javaPath = "main/java/" + MyStringUtils.replace(packageName, ".", "/");
/* 170 */     String mybatisPath = "main/resources/mapper/" + moduleName;
/* 171 */     String htmlPath = "main/resources/templates/" + moduleName + "/" + businessName;
/* 173 */     if (template.contains("domain.java.vm"))
/* 175 */       fileName = MyStringUtils.format("{}/domain/{}.java", new Object[] { javaPath, className });
/* 177 */     if (template.contains("sub-domain.java.vm") && MyStringUtils.equals("sub", genTable.getTplCategory())) {
/* 179 */       fileName = MyStringUtils.format("{}/domain/{}.java", new Object[] { javaPath, genTable.getSubTable().getClassName() });
/* 181 */     } else if (template.contains("mapper.java.vm")) {
/* 183 */       fileName = MyStringUtils.format("{}/mapper/{}Mapper.java", new Object[] { javaPath, className });
/* 185 */     } else if (template.contains("service.java.vm")) {
/* 187 */       fileName = MyStringUtils.format("{}/service/I{}Service.java", new Object[] { javaPath, className });
/* 189 */     } else if (template.contains("serviceImpl.java.vm")) {
/* 191 */       fileName = MyStringUtils.format("{}/service/impl/{}ServiceImpl.java", new Object[] { javaPath, className });
/* 193 */     } else if (template.contains("controller.java.vm")) {
/* 195 */       fileName = MyStringUtils.format("{}/controller/{}Controller.java", new Object[] { javaPath, className });
/* 197 */     } else if (template.contains("mapper.xml.vm")) {
/* 199 */       fileName = MyStringUtils.format("{}/{}Mapper.xml", new Object[] { mybatisPath, className });
/* 201 */     } else if (template.contains("list.html.vm")) {
/* 203 */       fileName = MyStringUtils.format("{}/{}.html", new Object[] { htmlPath, businessName });
/* 205 */     } else if (template.contains("list-tree.html.vm")) {
/* 207 */       fileName = MyStringUtils.format("{}/{}.html", new Object[] { htmlPath, businessName });
/* 209 */     } else if (template.contains("tree.html.vm")) {
/* 211 */       fileName = MyStringUtils.format("{}/tree.html", new Object[] { htmlPath });
/* 213 */     } else if (template.contains("add.html.vm")) {
/* 215 */       fileName = MyStringUtils.format("{}/add.html", new Object[] { htmlPath });
/* 217 */     } else if (template.contains("edit.html.vm")) {
/* 219 */       fileName = MyStringUtils.format("{}/edit.html", new Object[] { htmlPath });
/* 221 */     } else if (template.contains("sql.vm")) {
/* 223 */       fileName = businessName + "Menu.sql";
/*     */     } 
/* 225 */     return fileName;
/*     */   }
/*     */   
/*     */   public static String getProjectPath() {
/* 235 */     String packageName = GenConfig.getPackageName();
/* 236 */     StringBuffer projectPath = new StringBuffer();
/* 237 */     projectPath.append("main/java/");
/* 238 */     projectPath.append(packageName.replace(".", "/"));
/* 239 */     projectPath.append("/");
/* 240 */     return projectPath.toString();
/*     */   }
/*     */   
/*     */   public static String getPackagePrefix(String packageName) {
/* 251 */     int lastIndex = packageName.lastIndexOf(".");
/* 252 */     String basePackage = MyStringUtils.substring(packageName, 0, lastIndex);
/* 253 */     return basePackage;
/*     */   }
/*     */   
/*     */   public static HashSet<String> getImportList(GenTable genTable) {
/* 264 */     List<GenTableColumn> columns = genTable.getColumns();
/* 265 */     GenTable subGenTable = genTable.getSubTable();
/* 266 */     HashSet<String> importList = new HashSet<>();
/* 267 */     if (MyStringUtils.isNotNull(subGenTable))
/* 269 */       importList.add("java.util.List"); 
/* 271 */     for (GenTableColumn column : columns) {
/* 273 */       if (!column.isSuperColumn() && "Date".equals(column.getJavaType())) {
/* 275 */         importList.add("java.util.Date");
/* 276 */         importList.add("com.fasterxml.jackson.annotation.JsonFormat");
/*     */         continue;
/*     */       } 
/* 278 */       if (!column.isSuperColumn() && "BigDecimal".equals(column.getJavaType()))
/* 280 */         importList.add("java.math.BigDecimal"); 
/*     */     } 
/* 283 */     return importList;
/*     */   }
/*     */   
/*     */   public static String getPermissionPrefix(String moduleName, String businessName) {
/* 295 */     return MyStringUtils.format("{}:{}", new Object[] { moduleName, businessName });
/*     */   }
/*     */   
/*     */   public static String getParentMenuId(JSONObject paramsObj) {
/* 306 */     if (MyStringUtils.isNotEmpty((Map)paramsObj) && paramsObj.containsKey("parentMenuId") &&
/* 307 */       MyStringUtils.isNotEmpty(paramsObj.getString("parentMenuId")))
/* 309 */       return paramsObj.getString("parentMenuId"); 
/* 311 */     return "3";
/*     */   }
/*     */   
/*     */   public static String getTreecode(JSONObject paramsObj) {
/* 322 */     if (paramsObj.containsKey("treeCode"))
/* 324 */       return MyStringUtils.toCamelCase(paramsObj.getString("treeCode"));
/* 326 */     return "";
/*     */   }
/*     */   
/*     */   public static String getTreeParentCode(JSONObject paramsObj) {
/* 337 */     if (paramsObj.containsKey("treeParentCode"))
/* 339 */       return MyStringUtils.toCamelCase(paramsObj.getString("treeParentCode"));
/* 341 */     return "";
/*     */   }
/*     */   
/*     */   public static String getTreeName(JSONObject paramsObj) {
/* 352 */     if (paramsObj.containsKey("treeName"))
/* 354 */       return MyStringUtils.toCamelCase(paramsObj.getString("treeName"));
/* 356 */     return "";
/*     */   }
/*     */   
/*     */   public static int getExpandColumn(GenTable genTable) {
/* 367 */     String options = genTable.getOptions();
/* 368 */     JSONObject paramsObj = JSONObject.parseObject(options);
/* 369 */     String treeName = paramsObj.getString("treeName");
/* 370 */     int num = 0;
/* 371 */     for (GenTableColumn column : genTable.getColumns()) {
/* 373 */       if (column.isList()) {
/* 375 */         num++;
/* 376 */         String columnName = column.getColumnName();
/* 377 */         if (columnName.equals(treeName))
/*     */           break; 
/*     */       } 
/*     */     } 
/* 383 */     return num;
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-generator-4.7.0.jar!\com\cr\generato\\util\VelocityUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */